# -*- coding: utf-8 -*-
"""
# @created on : 9月 2021/9/15 16:09
# @author     : kevin
# @file       : header.py
# @software   : PyCharm
# @project    : auto_interface_test


@license: (C) Copyright 2019-2025, Corporation Limited.
@desc:
"""
accept_type = [{'css': 'ext/css,*/*;q=0.1'},
               {'txt': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'},
               {'img': 'image/webp,image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5'},
               {'json': 'application/json, text/javascript, */*; q=0.01'},
               {'other': '*/*'}]


class MyHeader:
    def __init__(self):
        self.header = None
        globals()['headers'] = {"Accept-Encoding": "gzip, deflate",
              "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15",
              "Accept-Language": "zh-cn",
              "Connection": "keep-alive",
              "Accept": "*/*"}

    def up_header(self, dic=None, header=None):
        """
            更新header
            :param dic: 需要添加进header的一个键值对，如果不需要修改则参数为空
            :return: 修改后的header
            """
        if header:
            self.header = header
        else:
            self.header = globals()['headers']
        if dic:
            for x in dic:
                self.header[x] = dic[x]

        return self.header

    def set_accept(self, type, header=None):
        """
        根据请求资源类型，更新header里的accept值
        :param type:希望资源类型
        :return:返回成功
        """
        type_len = len(accept_type)
        for i in range(0, type_len):
            if type in accept_type[i]:
                accept_value = accept_type[i][type]
                self.up_header({'Accept': accept_value}, header)
                return True

    def get_header(self, type, header=None):
        """
        根据请求资源类型，更新header并返回新的header
        :param type: 希望的资源类型
        :return: 相应资源类型的请求header
        """
        self.set_accept(type, header)
        return self.header

